package com.kunminx.examplegreendao;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.kunminx.examplegreendao.green_dao.BeanTouchDao;
import com.kunminx.examplegreendao.green_dao.DaoTouchManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //判断是否有读取权限
        havePermission();
        mBeanTouchDao = DaoTouchManager.getInstance().getDaoSession().getBeanTouchDao();
    }
    private BeanTouchDao mBeanTouchDao;

    /**
     * 增加
     * @param view
     */
    public void add(View view) {
        for (int i = 0; i < 10; i++) {
            String name="孙伟豪";
            int size=100;
            String listTouch="sdfsdfsdf";
            BeanTouch mBeanTouch=new BeanTouch(null,name,size,listTouch);
            mBeanTouchDao.insert(mBeanTouch);
        }

    }

    /**
     * 删除
     * @param view
     */
    public void delete(View view) {
        mBeanTouchDao.deleteByKey((long) 3);//根据Long来查询
    }

    /**
     * TODO:根据输入的beanTouch的id来改
     * @param view
     */
    public void update(View view) {
        BeanTouch beanTouch=new BeanTouch((long) 7,"孙伟豪改",200,"改变成功");
        mBeanTouchDao.update(beanTouch);
    }

    private static final String TAG = "MainActivity";
    //根据第几个来查询
    public void query(View view) {
        BeanTouch beanTouch = mBeanTouchDao.queryBuilder().build().list().get(0);
        Log.d(TAG, "query: beanTouch:"+beanTouch.getName());
    }
    //判断权限
    private void havePermission() {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED||
                ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        !=PackageManager.PERMISSION_GRANTED){
            //申请一个或多个权限，并提供用于回调的获取码
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            },100);
        }
    }
    /**
     * 权限请求结果
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //TODO:根据请求结果来做事
        Log.d(TAG, "onRequestPermissionsResult: requestcode"+requestCode
                +"\n"+permissions.toString()
                +"\n"+grantResults[0]
        );
        if (grantResults[0]!=0){
            Toast.makeText(this, "要开启权限才能用软件哦，不然无法让您更好的体验", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }

}